import axios from './index'
import type { AxiosResponse } from 'axios'
import type { UserInfo } from '../types/auth'

/**
 * 获取当前用户信息
 */
export const getCurrentUserInfo = (): Promise<AxiosResponse<UserInfo>> => {
  return axios.get('/api/users/me')
}

/**
 * 更新当前用户信息
 * @param userData 用户更新数据
 */
export interface UserUpdateData {
  nickname?: string;
  email?: string;
  phone?: string;
  avatarUrl?: string;
}

export interface UserUpdateResponse {
  success: boolean;
  message: string;
  data: UserInfo;
}

export const updateUserInfo = (userData: UserUpdateData): Promise<AxiosResponse<UserUpdateResponse>> => {
  return axios.put('/api/users/me', userData)
}

/**
 * 修改密码请求参数
 */
export interface ChangePasswordRequest {
  currentPassword: string;
  newPassword: string;
}

/**
 * 修改密码响应
 */
export interface ChangePasswordResponse {
  success: boolean;
  message: string;
}

/**
 * 修改用户密码
 * @param passwordData 密码修改数据
 */
export const changePassword = (passwordData: ChangePasswordRequest): Promise<AxiosResponse<ChangePasswordResponse>> => {
  return axios.put('/api/users/password', passwordData)
}

// 更新用户信息
updateUserInfo(data: any) {
  return request({
    url: '/api/user/info',
    method: 'put',
    data
  })
},

// 上传头像
uploadAvatar(data: FormData) {
  return request({
    url: '/api/user/avatar',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data
  })
},